import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:flutter_pickers/time_picker/model/suffix.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:library_base/utils/date_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/utils/get_util.dart';



class TimePickAndConsigneeDialog extends StatefulWidget {
  const TimePickAndConsigneeDialog({
    Key? key,
  }) : super(key: key);

  @override
  State<TimePickAndConsigneeDialog> createState() => _TimePickAndConsigneeDialogState();
}

class _TimePickAndConsigneeDialogState extends State<TimePickAndConsigneeDialog> {
  DateTime? currentTime;

  var loadingTime = '';

  String? consignee = '';

  Function(String,String?)? filter;

  Function()? reset;

  TextEditingController goodsCountController = TextEditingController();

  FocusNode goodsCountFocusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    currentTime = Get.arguments['currentTime'];
    consignee = Get.arguments['consignee'];

    if(consignee!=null && consignee!.isNotEmpty){
      goodsCountController.text = consignee!;
    }

    filter = Get.arguments['filter'];
    reset = Get.arguments['reset'];
    if (currentTime != null) {
      loadingTime = DateUtil.formatDate(currentTime, format: DataFormats.zh_y_mo_d);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        GetHelper.back();
      },
      child: Material(
        color: Colors.transparent,
        child: Container(
          color: Colors.transparent,
          child: GestureDetector(
            onTap: () {},
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  width: 0.88.sw,
                  decoration: rs8,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      SizedBox(
                        width: double.infinity,
                        child: Stack(
                          children: [
                            Container(
                                padding: edgeV50H24,
                                alignment: Alignment.center,
                                child: Text('配送时间', style: normalBoldTextColor32)),
                            Positioned(
                              top: 0.w,
                              right: 24.w,
                              child: GestureDetector(
                                onTap: () => Get.back(),
                                child: Container(
                                  padding: edgeV50H24,
                                  child: LocalImage('common_close',
                                      package: Constant.packLib(), width: 26.r, height: 26.r),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                      line1,
                      gapH24,
                      buildItem(
                          onTap: (){
                            showDateTimePicker();
                          },
                          title: MultiLanguage.creationTime.tr,
                          hint: MultiLanguage.pleaseSelectCreationTime.tr,
                          content: currentTime == null ? '选择配送时间' : loadingTime,),

                      // GestureDetector(
                      //   onTap: () {
                      //     showDateTimePicker();
                      //   },
                      //   child: Container(
                      //       alignment: Alignment.center,
                      //       width: double.infinity,
                      //       margin: edgeLR24,
                      //       decoration: BoxDecoration(
                      //         color: ThemeUtil.getTheme().secondaryBgColor(),
                      //         borderRadius: const BorderRadius.all(Radius.circular(6.0)),
                      //       ),
                      //       child: Row(
                      //         mainAxisAlignment: MainAxisAlignment.center,
                      //         crossAxisAlignment: CrossAxisAlignment.center,
                      //         children: [
                      //           Expanded(
                      //             child: Container(
                      //               padding: EdgeInsets.symmetric(vertical: 30.w, horizontal: 24.w),
                      //               alignment: Alignment.center,
                      //               // width: double.infinity,
                      //               child: Text(
                      //                 currentTime == null ? '选择配送时间' : loadingTime,
                      //                 style: currentTime == null
                      //                     ? TextStyles.textSixthColor28
                      //                     : TextStyles.textNormal28,
                      //                 textAlign: TextAlign.start,
                      //               ),
                      //             ),
                      //           ),
                      //         ],
                      //       )),
                      // ),
                      gapH24,
                      _buildInputItem(
                        title: MultiLanguage.consignee.tr,
                        hint: MultiLanguage.pleaseEnterTheConsignee.tr,
                        controller: goodsCountController,
                        focusNode: goodsCountFocusNode,
                        // onChanged: _countChange,
                      ),
                      gapH24,
                      Container(
                        padding: edgeLR24,
                        child: Row(
                          children: [
                            Expanded(
                              child: GradientButton(
                                ThemeUtil.getTheme(),
                                radius: const BorderRadius.only(
                                    topLeft: Radius.circular(50), bottomLeft: Radius.circular(50)),
                                colors: ThemeUtil.getTheme().buttonLeftBgColor(),
                                height: 80.w,
                                text: MultiLanguage.reset.tr,
                                onPressed: () {
                                  GetHelper.back();
                                  reset?.call();
                                },
                              ),
                            ),
                            Expanded(
                              child: GradientButton(
                                ThemeUtil.getTheme(),
                                radius: const BorderRadius.only(
                                    topRight: Radius.circular(50),
                                    bottomRight: Radius.circular(50)),
                                height: 80.w,
                                text: MultiLanguage.filter.tr,
                                onPressed: () {
                                  if (currentTime == null) {
                                    ToastUtil.waring('如需指定过滤日期,请选择具体日期,否则重置');
                                    return;
                                  }
                                  GetHelper.back();
                                  filter?.call(
                                      DateUtil.formatDate(currentTime, format: DataFormats.y_mo_d),
                                      goodsCountController.text
                                  );
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                      gapH36,
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  showDateTimePicker() {
    PDuration selectDate;
    if (currentTime != null) {
      selectDate =
          PDuration(year: currentTime!.year, month: currentTime!.month, day: currentTime!.day);
    } else {
      selectDate = PDuration(
          year: DateTime.now().year, month: DateTime.now().month, day: DateTime.now().day);
    }

    var pickerStyle = PickerStyle(
      showTitleBar: true,
      title: Container(
        alignment: Alignment.center,
        width: double.infinity,
        child: Text(MultiLanguage.selectDeliveryTime.tr, style: normalTextColor34),
      ),
      menuHeight: 36,
      cancelButton: Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 8.w),
        child: Text(MultiLanguage.cancel.tr, style: normalTextColor28),
      ),
      commitButton: Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 8.w),
        child: Text(MultiLanguage.determine.tr, style: primaryTextColor28),
      ),
      textSize: 36.sp,
      headDecoration: BoxDecoration(
          color: ThemeUtil.getTheme().colorScheme.secondary,
          borderRadius:
              const BorderRadius.only(topLeft: Radius.circular(8), topRight: Radius.circular(8))),
      textColor: ThemeUtil.getTheme().primaryColor,
      backgroundColor: ThemeUtil.getTheme().colorScheme.secondary,
      itemOverlay: CupertinoPickerDefaultSelectionOverlay(
          background: ThemeUtil.getTheme().normalTextColor().withOpacity(0.05)),
    );
    Pickers.showDatePicker(
      context,
      selectDate: selectDate,
      mode: DateMode.YMD,
      suffix: Suffix.normal(),
      minDate: PDuration(year: 2020, month: 2, day: 10),
      maxDate: PDuration(second: 22),
      pickerStyle: pickerStyle,
      onConfirm: (p) {
        currentTime = DateTime(p.year!, p.month!, p.day!);
        loadingTime = DateUtil.formatDate(currentTime, format: DataFormats.zh_y_mo_d);
        setState(() {});
      },
      // onChanged: (p) => print(p),
    );
  }

  _buildInputItem({
    required String title,
    required String hint,
    TextEditingController? controller,
    FocusNode? focusNode,
    ValueChanged<String>? onChanged,
  }) {
    return Row(children: [
      Expanded(
          flex: 2,
          child:
          Text(title, style: normalTextColor28, textAlign: TextAlign.end)),
      gapW24,
      Expanded(
          flex: 7,
          child: buildInput(
            padding: edgeAll24,
            hintText: hint,
            keyboardType: TextInputType.text,
            controller: controller,
            focusNode: focusNode,
            textAlign: TextAlign.start,
            margin: const EdgeInsets.all(0),
            onChanged: onChanged,
          )),
      gapW24,
    ]);
  }

  buildItem(
      {GestureTapCallback? onTap, required String title, required String hint, String? content}) {
    return Row(children: [
      Expanded(
          flex: 2, child: Text(title, style: TextStyles.textNormal28, textAlign: TextAlign.end)),
      Gaps.hGap24,
      Expanded(
          flex: 7,
          child: GestureDetector(
            onTap: onTap,
            child: Container(
                padding: edgeAll24,
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(30.0)),
                    border:
                    Border.all(color: ThemeUtil.getTheme().secondaryTextColor(), width: 1.0)),
                child: Row(children: [
                  Expanded(
                      child: Text(content == null || content.isEmpty ? hint : content,
                          style: content == null || content.isEmpty
                              ? TextStyles.textSecondary28
                              : TextStyles.textNormal28)),
                  LocalImage('common_grey_rightward_arrow',
                      package: Constant.packLib(), width: 20.w, height: 20.w),
                ])),
          )),
      gapW24,
    ]);
  }

}
